package com.snape.service.impl;

import com.snape.dao.RoomDao;
import com.snape.entity.Room;
import com.snape.entity.Room_Category;
import com.snape.po.RoomPo;
import com.snape.service.RoomService;
import com.snape.utils.PageLimitUtils;
import com.snape.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by liuqingcan_snape on 2017/4/2.
 */
@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao roomDao;


    /**
     * 根据分类名称查询分类主键
     * @param name
     * @return categoryId
     * @throws Exception
     */
    public int selectCategoryIdByRoomCategoryName(String name){

        int reId = -1;
        if (name==null ){

            return reId;
        }

        try {
            reId = roomDao.selectCategoryIdByRoomCategoryName(name);

        } catch (Exception e) {
            e.printStackTrace();

        }

        return  reId;
    }

    /**
     * 根据分类名称查询分类实体（理论上应该使用分类主键的）
     * @param name
     * @return Room_Category实体
     * @throws Exception
     */
    public Map<String,Object> selectCategoryInfoByCategoryName(String name){

        int reId = -1;

        Map<String,Object> result = new HashMap<String, Object>();
        if (name==null ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            reId = this.selectCategoryIdByRoomCategoryName(name);

            Room_Category category = roomDao.selectCategoryInfoByCategoryName(name);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",category);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     * 根据分类主键查询分类实体 （理论上应该使用当前Api）
     * @param id
     * @return Room_Category实体
     * @throws Exception
     */
    public Map<String,Object> selectCategoryInfoByCategoryId(int id){
        Map<String,Object> result = new HashMap<String, Object>();
        if (id<=0){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            Room_Category category = roomDao.selectCategoryInfoByCategoryId(id);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",category);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    public Map<String,Object> selectAllCategoryInfo(){
        Map<String,Object> result = new HashMap<String, Object>();

        try {
            List<Room_Category> category = roomDao.selectAllCategory();
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",category);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }


    /**
     * 插入一条room实体
     * @param room
     * @return
     */
    public Map<String,Object> insertRoomInfo(Room room){
        Map<String,Object> result = new HashMap<String, Object>();

        try {
            if(room.getValidTime() == null){
                room.setValidTime("有效期至2016-06-10");
            }
            roomDao.insertRoomInfo(room);
            Integer roomId = roomDao.selectLastId();

            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",roomId);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }


    /**
     * 分页查询,按时间排序查询出该分类下最优先的10条数据
     * @param id
     * @return List<Room>
     * @throws Exception
     */
    public Map<String,Object> selectOnePageRoomByCategoryId(int id){
        Map<String,Object> result = new HashMap<String, Object>();
        if (id<=0){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            List<Room> roomList = roomDao.selectOnePageRoomByCategoryId(id);
            List<RoomPo> roomLists = new ArrayList<RoomPo>();
            for (Room room : roomList){
                RoomPo roomPo = new RoomPo();
                String fe = room.getFeature();
                if (fe != null){
                    String [] feArr = fe.split("###");
                    roomPo.setFeature(feArr);
                }
//                String [] feArr = fe.split("###");
                roomPo.setCategory_id(room.getCategory_id());
                roomPo.setClasshour(room.getClasshour());

                roomPo.setId(room.getId());
                roomPo.setImage(room.getImage());
                roomPo.setValidTime(room.getValidTime());
                roomPo.setTitle(room.getTitle());
                roomPo.setPrice(room.getPrice());

                roomLists.add(roomPo);
            }
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",roomLists);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     *  根据分页number,查询出视频房间(课程)数据
     * @param id
     * @param pageNumber
     * @param postNumber
     * @return
     * @throws Exception
     */
    public Map<String,Object> selectRoomByCategotyidAndPagenumber(int id ,int pageNumber,int postNumber){
        Map<String,Object> result = new HashMap<String, Object>();
        if (id<=0 || pageNumber<0 ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            PageLimitUtils limitUtils = new PageLimitUtils();
            int realDbSum = roomDao.selectCountRoomByCategoryId(id);
            int end = limitUtils.countEndNumber(pageNumber,postNumber,realDbSum);
            int start = limitUtils.countStartNumber(pageNumber,postNumber,realDbSum);


            List<Room> rooms = roomDao.selectRoomByCategotyidAndPagenumber(id,start,end);

            if (rooms.size()>0) {
                List<RoomPo> roomLists = new ArrayList<RoomPo>();
                for (Room room : rooms){
                    RoomPo roomPo = new RoomPo();
                    String fe = room.getFeature();
                    String [] feArr = fe.split("###");
                    roomPo.setCategory_id(room.getCategory_id());
                    roomPo.setClasshour(room.getClasshour());
                    roomPo.setFeature(feArr);
                    roomPo.setId(room.getId());
                    roomPo.setImage(room.getImage());
                    roomPo.setValidTime(room.getValidTime());
                    roomPo.setTitle(room.getTitle());
                    roomPo.setPrice(room.getPrice());

                    roomLists.add(roomPo);
                }
                result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
                result.put("data",roomLists);
                result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());


            }else {
                result.put("statusCode", StatusUtils.SELECT_DATA_NONE.getStatusCode());
                result.put("data", rooms);
                result.put("statusMsg", StatusUtils.SELECT_DATA_NONE.getStatusName());
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     *  查询出roomID,以此可以取出属于该room的所有详细课程信息
     * @param name
     * @return
     * @throws Exception
     */
    public Map<String,Object> selectRoomIdByRoomName(String name){
        Map<String,Object> result = new HashMap<String, Object>();
        if (name == null ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            Integer roomid = roomDao.selectRoomIdByRoomName(name);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",roomid);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     * 查询课程有效期
     * @param id
     * @return
     * 返回的是date格式 (new ex: Sun Apr 23 00:00:00 CST 2017)
     * @throws Exception
     */
    public Map<String,Object> selectValidDateByRoomId(int id){
        Map<String,Object> result = new HashMap<String, Object>();
        if (id <= 0){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            String date = roomDao.selectValidDateByRoomId(id);
            if (date != null){
//                date = date.substring(4);
//                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                Date reDate = format.parse(date);
//                System.out.print(reDate);
                result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
                result.put("data",date);
                result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
            }else {
                result.put("statusCode",StatusUtils.SELECT_DATA_NONE.getStatusCode());
                result.put("data",null);
                result.put("statusMsg",StatusUtils.SELECT_DATA_NONE.getStatusName());
            }


        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }
}
